Ext.ns('WaterUpdateRecordView');

WaterUpdateRecordView = Ext.extend(Ext.Panel, {
    // 头部工具栏
    topbar : null,
    // 数据展示Panel
    gridPanel : null,
    // GridPanel的数据Store
    store : null,
    // 构造函数
    constructor : function(_cfg) {
        Ext.applyIf(this, _cfg);
        // 初始化组件
        this.initUIComponents();
        // 调用父类构造
        WaterUpdateRecordView.superclass.constructor.call(this, {
                    id : 'WaterUpdateRecordView',
                    title : '水表电话抄表',
                    region : 'center',
                    layout : 'border',
                    items : [this.searchPanel,this.gridPanel]
                });
        
    },// end of constructor

    // 初始化组件
    initUIComponents : function() {      
    	// 初始化搜索条件Panel
        this.searchPanel = new Ext.FormPanel({
            id : 'WaterUpdateSearchForm',
            height : 120,
            region : 'north',
            frame : false,
            border : false,
            padding : '10',
            layout : 'column',
            title : '查询条件',
            labelWidth : 60,
            enctype : 'multipart/form-data',
            fileUpload : true,
            url : __ctxPath + '/pdaweb/checkXLSFormatPdaWaterList.do',
            layoutConfig : {
                padding : '5',
                align : 'middle'
            },
            defaults : {
				columnWidth : 0.5,
				layout : 'form',
				border : false,
				style : 'padding-left:10px;',
				defaults : {
					xtype : 'textfield',
					width : 130
				}
			},
			items : [ {
				items : [ {
					fieldLabel : '抄表年月',
					xtype : 'monthfield',
					value : new Date(),
					editable : false,
					allowblank : false,
					name : 'Q_month_S_LK'
				}, {
					fieldLabel : '区',
					hiddenName : 'Q_pdaWaterMeter.districtId_S_LK',
					xtype : 'combo',
					id : 'pdaWaterMeter.districtID',
					valueField : 'districtId',
					displayField : 'districtName',
					mode : 'local',
					editable : false,
					triggerAction : 'all',
					store : new Ext.data.SimpleStore({
								url : __ctxPath
										+ '/pdaweb/showDistrictAmPlan.do',
								autoLoad : true,
								fields : ['districtId','districtName']
							}),
					listeners : {
						scope : this,
						select : function(cbo,record, index) {
							var b = Ext.getCmp('pdaWaterMeter.buildingID');
							var h = Ext.getCmp('pdaWaterMeter.houseID');
							b.setValue('');
							b.store.loadData([]);
							h.setValue('');
							h.store.loadData([]);
							
							chooseDistrictId = cbo.getValue();
							Ext.Ajax.request({
								url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId='
										+ chooseDistrictId,
								method : 'post',
								success : function(response,option) {
									b.store.loadData(eval("("+ response.responseText
													+ ")"));
								}
							});
						}
					}
				
				},{
					fieldLabel : '房',
					width : 220,
					hiddenName : 'Q_pdaWaterMeter.aparmentId_S_LK',
					xtype : 'combo',
					id : 'pdaWaterMeter.houseID',
					valueField : 'houseId',
					displayField : 'houseName',
					mode : 'local',
					editable : false,
					triggerAction : 'all',
					store : new Ext.data.SimpleStore({
								url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId=',
								//autoLoad : true,
								fields : ['houseId','houseName']
							}),
					listeners : {
						scope : this,
						select : function(cbo,record, index) {
						}
					}
				} ]
			}, {
				items : [ {
					fieldLabel : '水表编号',
					id : 'pdaWaterMeter.wmId',
					name : 'Q_amId_S_LK'
				}, {
					fieldLabel : '栋',
					hiddenName : 'Q_pdaWaterMeter.buildingId_S_LK',
					xtype : 'combo',
					id : 'pdaWaterMeter.buildingID',
					valueField : 'buildingId',
					displayField : 'buildingtName',
					mode : 'local',
					editable : false,
					triggerAction : 'all',
					store : new Ext.data.SimpleStore({
								url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId=',
								//autoLoad : true,
								fields : ['buildingId','buildingtName']
							}),
					listeners : {
						scope : this,
						select : function(cbo,record, index) {
							var h = Ext.getCmp('pdaWaterMeter.houseID');
							h.setValue('');
							h.store.loadData([]);
							chooseBulidingId = cbo.getValue();
							Ext.Ajax.request({
								url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId='
										+ chooseBulidingId,
								method : 'post',
								success : function(response,option) {
									h.store.loadData(eval("("+ response.responseText+ ")"));
								}
							});
						}
					}
				
				}]
			}, {
				items : [{
					xtype:'hidden',
					name : 'Q_degree_L_EQ',
					value: 0
				}/* {
					fieldLabel : '记录状态',
					hiddenName : 'Q_status_S_EQ',
					xtype : 'combo',
					valueField : 'status',
					displayField : 'statusData',
					mode : 'local',
					width : 100,
					editable : false,
					triggerAction : 'all',
					value : '',
					store : [ [ '', '所有' ],[ '2', '异常' ] ]
				} */]
			} ]
        });// end of the searchPanel
        
        this.store = new Ext.data.JsonStore({
        	url : __ctxPath + '/pdaweb/listPdaWaterList.do',
            root : 'result',
            totalProperty : 'totalCounts',
            fields : [{
                        name : 'id',
                        type : 'int'
                    }, 'status', 'amId', 'lastDegree', 'transcribeType',
                    'degree', 'pdaWaterMeter.districtId',
                    'pdaWaterMeter.buildingId',
                    'pdaWaterMeter.aparmentId',
                    'recorderId', 'month']
        });// end of the store
        
        var cm = new Ext.grid.ColumnModel({
            columns : [new Ext.grid.RowNumberer(), {
                        header : '水表编号',
                        width : 120,
                        dataIndex : 'amId'
                    }, {
                        header : '上月读数',
                        width : 80,
                        dataIndex : 'lastDegree', 
                        renderer : function(val) {
                            return val == -1 ? "" : val;
                        }
                    }, {
                        header : '读数',
                        width : 50,
                        dataIndex : 'degree'
                    }/*, {
                        header : '抄录类型',
                        width : 65,
                        dataIndex : 'transcribeType', 
                        renderer : function(val) {
                            return val == 0 ? "无" : (val == 1 ? "手动" : "pda");
                        }
                    }*/, {
                        header : '区号',
                        width : 80,
                        dataIndex : 'pdaWaterMeter.districtId'
                    }, {
                        header : '栋号',
                        width : 100,
                        dataIndex : 'pdaWaterMeter.buildingId'
                    }, {
                        header : '房号',
                        width : 150,
                        dataIndex : 'pdaWaterMeter.aparmentId'
                    }, {
                        header : '抄表人',
                        width : 60,
                        dataIndex : 'recorderId'
                    }, {
                        header : '抄表年月',
                        width : 80,
                        dataIndex : 'month'
                    }, {
                        header : '管理',
                        width : 50,
                        dataIndex : 'id',
                        sortable : false,
                        renderer : function(value, metadata, record, rowIndex,
                                colIndex) {
                            var d = record.data;
                            var recordStr = d.amId + ";" + d.lastDegree + ";"+ d.degree + ";"
                                    + record.get("pdaWaterMeter.districtId") + ";"
                                    + record.get("pdaWaterMeter.buildingId") + ";"
                                    + record.get("pdaWaterMeter.aparmentId")
                                    + ";" +(d.recorderId==null?"":d.recorderId)+ ";" + d.month + ";"
                                    + d.status;

                            return '&nbsp;<button title="编辑" value=" " class="btn-edit" '
                                    + 'onclick="WaterUpdateRecordView.edit('
                                    + record.data.id
                                    + ",\'"
                                    + recordStr
                                    + '\');">&nbsp</button>';
                        }
                    }],
            defaults : {
                menuDisabled : false,
                width : 120
            }
        });// end of the cm

        this.topbar = new Ext.Toolbar({
            height : 30,
            bodyStyle : 'text-align:left',
            items : [new Ext.Button({
                        iconCls : 'search',
                        text : '查询',
                        handler : function() {
                            var searchPanel = Ext
                                    .getCmp('WaterUpdateSearchForm');
                            var gridPanel = Ext
                                    .getCmp('WaterUpdateRecordGrid');
                            if (searchPanel.getForm().isValid()) {
                                $search({
                                            searchPanel : searchPanel,
                                            gridPanel : gridPanel
                                        });
                            }
                        }
                    }),{
    					xtype : 'button',
    					text : '重置',
    					iconCls : 'btn-reset',
    					handler : function() {
    						Ext.getCmp('pdaWaterMeter.wmId').setValue('');
    						Ext.getCmp('pdaWaterMeter.districtID').setValue('');
    						Ext.getCmp('pdaWaterMeter.buildingID').setValue('');
    						Ext.getCmp('pdaWaterMeter.houseID').setValue('');
//    						Ext.getCmp('WaterUpdateSearchForm').getForm().reset();
    					}
    				
    				}]
        });

        this.gridPanel = new Ext.grid.GridPanel({
                    id : 'WaterUpdateRecordGrid',
                    tbar : this.topbar,
                    region : 'center',
                    store : this.store,
                    trackMouseOver : true,
                    disableSelection : false,
                    loadMask : true,
                    autoHeight : true,
                    cm : cm,
                    bbar : new Ext.PagingToolbar({
                                pageSize : 25,
                                store : this.store,
                                displayInfo : true,
                                displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
                                emptyMsg : "当前没有记录"
                            })
                });
        
    }
});


// 编辑
WaterUpdateRecordView.edit = function(recId, recordStr) {
    new WaterUpdateRecordForm({
                recId : recId,
                recordStr : recordStr
            }).show();
};
WaterUpdateRecordView.checkAbnormal = function(status) {
    if (status == '2' || status == '3' || status == '4') {
        return true;
    }
    return false;
};
